package com.software.practicecenter.controller;

import com.software.practicecenter.entity.*;
import com.software.practicecenter.service.MasterLevelService;
import com.software.practicecenter.service.PracticeRecordService;
import com.software.practicecenter.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @Author xiaoliao
 * @Date 2023/6/2 8:31
 */
@RestController
@RequestMapping("/practice_record")
@RefreshScope
public class PracticeRecordController {
    @Autowired
    private PracticeRecordService practiceRecordService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private MasterLevelService masterLevelService;

    @GetMapping("/findById/{id}")
    public PracticeRecord findById (@PathVariable String id) {
        return practiceRecordService.findById(id);
    }

    @GetMapping("/findByUserId/{userId}")
    public List<PracticeRecord> findByUserId(@PathVariable String userId){
        return practiceRecordService.findByUserId(userId);
    }

    @GetMapping("/findRecordsByUserId/{userId}")
    public List<RecordDTO> findRecordsByUserId(@PathVariable String userId){
        return practiceRecordService.findRecords(userId);
    }

    @PostMapping("/addRecord")
    public void addRecord(@RequestBody PracticeRecord practiceRecord){
        practiceRecordService.addRecord(practiceRecord);
        String questions = practiceRecord.getQuestions();
        String userAnswer = practiceRecord.getAnswers();
        String userId = practiceRecord.getUserId();

        String[] strQuestions = questions.split(",");
        List<String> questionIds = new ArrayList<>();
        Collections.addAll(questionIds, strQuestions);
        List<Question> questionList = questionService.findByIdIn(questionIds);

        String strC = "";
        String strK = "";
        for (Question question : questionList) {
            strK += question.getKnowledgePoints() + ",";
            strC += question.getCorrectOption() + ",";
        }
        String[] strUserAnswers = userAnswer.split(",");
        String[] strKnowledge = strK.split(",");
        String[] strCorrectOption = strC.split(",");

        List<String> userAnwers = new ArrayList<>();
        List<String> knowledgeIds = new ArrayList<>();
        List<String> correctOptions = new ArrayList<>();

        Collections.addAll(userAnwers, strUserAnswers);
        Collections.addAll(knowledgeIds, strKnowledge);
        Collections.addAll(correctOptions, strCorrectOption);

        List<MasterLevel> masterLevelList = masterLevelService.findByUserIdAndKnowledgeIdIn(userId, knowledgeIds);
        for (int i = 0; i < userAnwers.size(); i++) {
            if (userAnwers.get(i).equals(correctOptions.get(i))) {
                int flag = 0;
                for (MasterLevel masterLevel : masterLevelList){
                    if (masterLevel.getKnowledgeId().equals(knowledgeIds.get(i))) {
                        String[] oldLevel = masterLevel.getMasterLevel().split(",");;
                        int x = Integer.valueOf(oldLevel[0]) + 1;
                        int y = Integer.valueOf(oldLevel[1]) + 1;
                        String newLevel = x + "," + y;
                        masterLevel.setMasterLevel(newLevel);
                        flag = 1;
                    }
                }
                if (flag == 0){
                    MasterLevel masterLevel = new MasterLevel();
                    masterLevel.setId(UUID.randomUUID().toString());
                    masterLevel.setUserId(userId);
                    masterLevel.setKnowledgeId(knowledgeIds.get(i));
                    masterLevel.setMasterLevel("1,1");
                    masterLevelList.add(masterLevel);
                }
            } else {
                int flag = 0;
                for (MasterLevel masterLevel : masterLevelList){
                    if (masterLevel.getKnowledgeId().equals(knowledgeIds.get(i))) {
                        String[] oldLevel = masterLevel.getMasterLevel().split(",");;
                        int x = Integer.valueOf(oldLevel[0]);
                        int y = Integer.valueOf(oldLevel[1]) + 1;
                        String newLevel = x + "," + y;
                        masterLevel.setMasterLevel(newLevel);
                        flag = 1;
                    }
                }
                if (flag == 0){
                    MasterLevel masterLevel = new MasterLevel();
                    masterLevel.setId(UUID.randomUUID().toString());
                    masterLevel.setUserId(userId);
                    masterLevel.setKnowledgeId(knowledgeIds.get(i));
                    masterLevel.setMasterLevel("0,1");
                    masterLevelList.add(masterLevel);
                }
            }
        }
        masterLevelService.saveAll(masterLevelList);
    }

    @GetMapping("/findAllByUserId/{userId}")
    public AllRecord findAllByUserId(@PathVariable String userId){
        return practiceRecordService.findAllByUserId(userId);
    }
}
